﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using Server;

namespace Services
{
    public class MyServiceHost : ServiceHost
    {
        public MyServiceHost(Controller controller, Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {

            if (controller == null)
            {
                throw new ArgumentNullException("dep");
            }

            foreach (var cd in this.ImplementedContracts.Values)
            {
                cd.Behaviors.Add(new MyInstanceProvider(controller));
            }
        }
    }


    public class MyInstanceProvider : IInstanceProvider, IContractBehavior
    {
        private readonly Controller controller;

        public MyInstanceProvider(Controller controller)
        {
            if (dep == null)
            {
                throw new ArgumentNullException("controller");
            }

            this.controller = controller;
        }

        #region IInstanceProvider Members

        public object GetInstance(InstanceContext instanceContext, Message message)
        {
            return this.GetInstance(instanceContext);
        }

        public object GetInstance(InstanceContext instanceContext)
        {
            return new WebServices(this.controller);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object instance)
        {
        }

        #endregion

        #region IContractBehavior Members

        public void AddBindingParameters(ContractDescription contractDescription, ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint, DispatchRuntime dispatchRuntime)
        {
            dispatchRuntime.InstanceProvider = this;
        }

        public void Validate(ContractDescription contractDescription, ServiceEndpoint endpoint)
        {
        }

        #endregion
    }

    class WebServices : IWebServices
    {
        private Controller _controller;

        public WebServices(Controller controler)
        {
            _controller = controler;

        }

        public Guid RegisterComputingUnit(string processorName, short logicalProcessor)
        {
            OperationContext context = OperationContext.Current;
            MessageProperties prop = context.IncomingMessageProperties;
            RemoteEndpointMessageProperty endpoint =
                prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
            string ip = endpoint.Address;
            return _controller.RegisterComputingUnit(new Processor { Name = processorName, LogicalProcess = logicalProcessor }, ip);
        }
    }
}
